import {Component, OnInit} from '@angular/core';
import {HttpService} from '@core/services/http.service';
import {NzModalService} from 'ng-zorro-antd';
import swal from 'sweetalert2';

@Component({
    selector: 'app-student',
    templateUrl: './student.component.html',
    styleUrls: ['./student.component.less']
})
export class StudentComponent implements OnInit {
    _value = '';

    _allChecked = false;
    _disabledButton = true;
    _checkedNumber = 0;
    _displayData: Array<any> = [];
    _dataSet = [];
    _indeterminate = false;

    sch_id = JSON.parse(localStorage.getItem('_user')).sch_id;
    order = 'stu_id';
    total;
    isRemove = false;
    style: any = {
        width: '70%',
        top: '190px'
    };

    sortMap = {
        stu_name: null,
        major_name: null,
        cla_name:null
    };
    sortName = null;
    sortValue = null;
    data = [];
    stuArr = [];

    key = '';
    constructor(public http: HttpService,public nzModal: NzModalService) {
    }

    _displayDataChange($event) {
        this._displayData = $event;
    }

    _refreshStatus() {
        const allChecked = this._displayData.every(value => value.checked === true);
        const allUnChecked = this._displayData.every(value => !value.checked);
        this._allChecked = allChecked;
        this._indeterminate = (!allChecked) && (!allUnChecked);
        this._disabledButton = !this._dataSet.some(value => value.checked);
        this._checkedNumber = this._dataSet.filter(value => value.checked).length;
    }

    _checkAll(value) {
        if (value) {
            this._displayData.forEach(data => data.checked = true);
        } else {
            this._displayData.forEach(data => data.checked = false);
        }
        this._refreshStatus();
    }

    ngOnInit() {
        this.getData()
    }

    getData() {
        let param = {
            key:this.key,
            sch_id:this.sch_id,
            pi:1,
            ps:9999,
            order:this.order
        };

        this.http.getStudentBySchId(param).subscribe(res=>{
            //console.log(res);
            this.data = res['list'];
            this.total = res['total']
        })
    }

    sort(sortName, value) {
        this.sortName = sortName;
        this.sortValue = value;
        Object.keys(this.sortMap).forEach(key => {
            if (key !== sortName) {
                this.sortMap[ key ] = null;
            } else {
                this.sortMap[ key ] = value;
            }
        });
        if(this.sortValue === null){
            this.order = 'stu_id'
        }else{
            this.order = this.sortName +' '+ this.sortValue
        }
        this.getData();
    }

    resetPwd(){
        for(let list of this.data){
            if(list.checked){
                this.stuArr.push(list.stu_id)
            }
        }
        if(this.stuArr.length){
            this.http.resetPassWord({stuArr:this.stuArr}).subscribe(res=>{
                //console.log(res);
                if(res['code'] == 0){
                    swal('密码已重置为 123456','','success');
                    for(let list of this.data){
                        list.checked = false
                    }
                    this._refreshStatus();
                    this.stuArr = [];
                }else{
                    swal(res['msg'],'','info')
                }

            })

        }else{
            swal('请选中需要重置密码的学生','','info')
        }
    }
    stuJson;
    removeStuName
    isDelstu(data){
        //console.log(data)
        this.stuJson = data;
        this.removeStuName = data.stu_name
        this.isRemove = true
    }
    deleteStu(){
        this.http.deleteStudent({u_id:this.stuJson.u_id}).subscribe(res=>{

            if(res['code'] == 0){
                this.getData();
                this.isRemove = false;
                swal(res['msg'],'','success');
            }else{
                swal(res['msg'],'','info');
            }
        })
    }

    isModaltec = false;
    tecList = [];
    studentName;
    tecShow(data){
        //console.log(data);
        let maj_id = data.maj_id;
        this.studentName = data.stu_name;
        this.isModaltec = true;
        this.http.getTeacherInfoListByMajId({maj_id:maj_id}).subscribe(res=>{
            //console.log(res)
            this.tecList = res['list']
        })
    }
    doSearch() {
        if(!this._value){
            this._value = '';
        }
        this.key = this._value;
        this.getData();
    }

}
